import 'package:flutter/material.dart';

import 'change_notifier_provider.dart';
import 'child_widget.dart';
import 'view_model.dart';


class ProviderDemo extends StatefulWidget {
  @override
  _ProviderDemoState createState() => _ProviderDemoState();
}

class _ProviderDemoState extends State<ProviderDemo> {
  ViewModel _vm;
  @override
  void initState() {
    _vm = ViewModel();
    super.initState();
  }

  @override
  void dispose() {
    _vm.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Padding(
            padding: const EdgeInsets.only(bottom: 20.0),
            child: ChangeNotifierProvider<ViewModel>(
                data: _vm, child: ChildWidget()), //子widget中依赖ShareDataWidget
          ),
          RaisedButton(
            child: const Text('Increment'),
            //每点击一次，将count自增，然后重新build,ShareDataWidget的data将被更新
            onPressed: () {
              _vm.count = _vm.count + 1;
            },
          ),
          RaisedButton(
            child: const Text('改变文本'),
            //每点击一次，将count自增，然后重新build,ShareDataWidget的data将被更新
            onPressed: () {
              _vm.test+= '1';
            },
          )
        ],
      ),
    );
  }
}
